package com.wen.Part5;

import printer.BinaryTrees;

import java.util.LinkedList;
import java.util.List;

public class Tree {



    public static List<TreeNode> list = new LinkedList<>();


    static void f(CharNode root, CharNode index){
        if (index == null) return;

//        if (index.left != null && index.right != null && root != index){
//            System.out.print("(");
//            f(root,index.left);
//            System.out.print(index.val);
//            f(root,index.right);
//            System.out.print(")");
//        }else if(index.left == null && index.right != null){
//            f(root,index.left);
//            System.out.print("(");
//            System.out.print(index.val);
//            f(root,index.right);
//            System.out.print(")");
//        }else{
//            f(root,index.left);
//            System.out.print(index.val);
//            f(root,index.right);
//        }
        if (index.left != null && index.right != null && root != index){
            System.out.print("(");
        }
        f(root,index.left);
        if(index.left == null && index.right != null){
            System.out.print("(");
        }
        System.out.print(index.val);
        f(root,index.right);
        if (index.left != null && index.right != null && root != index || (index.left == null && index.right != null)){
            System.out.print(")");
        }
    }

    public static void main(String[] args) {
//        TreeNode root = new TreeNode(1);
//        TreeNode node2 = new TreeNode(2);
//        TreeNode node3 = new TreeNode(3);
//        TreeNode node4 = new TreeNode(4);
//        TreeNode node5 = new TreeNode(5);
//        root.left = node2;
//        root.right = node3;
//        node2.left = node4;
//        node2.right = node5;

//        CharNode root = new CharNode('*');
//        CharNode node1 = new CharNode('+');
//        CharNode node2 = new CharNode('*');
//        root.left = node1;
//        root.right = node2;
//
//
//        CharNode node3 = new CharNode('a');
//        CharNode node4 = new CharNode('b');
//        node1.left = node3;
//        node1.right = node4;
//
//        CharNode node5 = new CharNode('c');
//        CharNode node6 = new CharNode('-');
//        node2.left = node5;
//        node2.right = node6;
//        CharNode node7 = new CharNode('d');
//        node6.right = node7;
//
        CharNode root = new CharNode('+');
        CharNode node1 = new CharNode('*');
        CharNode node2 = new CharNode('-');
        root.left = node1;
        root.right = node2;


        CharNode node3 = new CharNode('a');
        CharNode node4 = new CharNode('b');
        node1.left = node3;
        node1.right = node4;

        CharNode node5 = new CharNode('-');
        node2.right = node5;

        CharNode node6 = new CharNode('c');
        CharNode node7 = new CharNode('d');
        node5.left = node6;
        node5.right = node7;



        BinaryTrees.println(root);
        f(root,root);
    }




}
